package com.usooft.metaTalent.admin.system.form;

import com.usooft.framework.core.query.Sort;
import com.usooft.framework.web.form.ListForm;
import com.usooft.metaTalent.core.system.entity.SearchTerm;
import com.usooft.metaTalent.core.system.query.SearchTermQuery;
import lombok.Setter;

import java.time.LocalDateTime;

/**
 * @author Xandy
 * @version 1.0.0
 * @description
 * @date 2023/10/25
 */
@Setter
public class SearchTermListForm extends ListForm<SearchTermQuery> {

    private LocalDateTime createdDateOnOrBefore;
    private LocalDateTime createdDateOnOrAfter;

    private SearchTerm.PortType searchTypeIs;

    private SearchTerm.Position positionIs;

    private String contentContains;

    private Boolean isTopIs;

    private Boolean isHotIs;

    private SearchTermQuery.Filter buildFilter() {
        return SearchTermQuery.Filter.builder()
                .createdDateOnOrAfter(createdDateOnOrAfter)
                .createdDateOnOrBefore(createdDateOnOrBefore)
                .contentContains(contentContains)
                .portTypeIs(searchTypeIs)
                .positionIs(positionIs)
                .isTopIs(isTopIs)
                .isHotIs(isHotIs)
                .build();
    }

    private SearchTermQuery.Sorts buildSorts() {
        SearchTermQuery.Sorts.Builder builder = SearchTermQuery.Sorts.builder();
        for (String sortItem : getSortItems()) {
            Sort.Order order = getSortOrder(sortItem);
            switch (sortItem) {
                case "-createdDate", "createdDate" -> builder.id(order);
            }
        }
        return builder.build();
    }

    private SearchTermQuery.Wraps buildWraps() {
        return SearchTermQuery.Wraps.builder()
                .build();
    }

    public SearchTermQuery buildQuery() {
        return SearchTermQuery.builder()
                .filter(buildFilter())
                .wraps(buildWraps())
                .sorts(buildSorts())
                .limit(getLimit())
                .build();
    }

}
